<?php
namespace Admin\Controller;

class IndexController extends CommonController
{
    /**
     * @后台首页
     *
     * @param void
     * @return void
     */
    public function index()
    {
        $module = getModule(false);
        $rules = D('Admin')->getAdminRule(ADMIN_ID);
        foreach ($module as $key => $data) {
            if (in_array($data['module_id'], $rules) && $data['console'] == 0){
                $parentId =  $data['parent_id'];
                if (0 == $parentId) {
                    $menu[] =& $module[$key];
                }else{
                    if (isset($module[$parentId])) {
                        $parent =& $module[$parentId];
                        $parent['children'][] =& $module[$key];
                    }
                }               
            }
        }
        $menu[0]['current'] = 1;
        $admin = session('admin_auth');
        $this->assign('admin', $admin);
        $this->assign('menu', $menu);
        $this->display();
    }

    /**
     * @默认欢迎页面
     *
     * @param void
     * @return void
     */
    public function welcome()
    {
        $this->display();
    }

    /**
     * @ajax && kindeditor编辑器上传
     *
     * @param void
     * @return json
     */
    public function upload()
    {
        $file = D('File');
        $info = $file->upload($_FILES, C('PICTURE_UPLOAD'));
        $info = end($info);
        if ($info) {
            $return['error'] = 0;
            $return['url'] = $info['url'];
        } else {
            $return['error'] = 1;
            $return['message'] = $file->getError();
        }
        exit(json_encode($return));
    }
    
    /**
     * @默认欢迎页面
     *
     * @param void
     * @return void
     */
    public function statistics()
    {
        if (function_exists ( 'gd_info' )) {
            $gd = gd_info ();
            $ecgd = $gd ['GD Version'];
        } else {
            $ecgd = '不支持';
        }
    
        $info = array (
            '操作系统' => PHP_OS,
            '运行环境' => $_SERVER ["SERVER_SOFTWARE"],
            'PHP运行方式' => php_sapi_name (),
            '上传附件限制' => ini_get ( 'upload_max_filesize' ),
            '执行时间限制' => ini_get ( 'max_execution_time' ) . '秒',
            '服务器时间' => date ( "Y年n月j日 H:i:s" ),
            '北京时间' => gmdate ( "Y年n月j日 H:i:s", time () + 8 * 3600 ),
            '服务器域名/IP' => $_SERVER ['SERVER_NAME'] . ' [ ' . gethostbyname ( $_SERVER ['SERVER_NAME'] ) . ' ]',
            '剩余空间' => round ( (@disk_free_space ( "." ) / (1024 * 1024)), 2 ) . 'M',
            'register_globals' => get_cfg_var ( "register_globals" ) == "1" ? "ON" : "OFF",
            'magic_quotes_gpc' => (1 === get_magic_quotes_gpc ()) ? 'YES' : 'NO',
            'magic_quotes_runtime' => (1 === get_magic_quotes_runtime ()) ? 'YES' : 'NO',
            'mysql' => function_exists ( "mysql_close" ) ? mysql_get_client_info () : '不支持',
            'alow' => ini_get ( 'allow_url_fopen' ) ? '支持' : '不支持',
            'ecgd' => $ecgd
        );
    
        $this->assign('info', $info);
        $this->display();
    }

    /**
     * @更新缓存
     *
     * @param void
     * @return json
     */
    public function cache()
    {
        if (IS_POST) {
            $cache = I('post.cache');
            $full = I('post.cls_full');
            if ($full) {
                $config = M('Setting')->field('set_key')->select();
                foreach ($config as $val) {
                    getConfig($val['set_key'], false);
                }
                getAccessToken(false);
                getCouponList(false);
                getGoodsList(false);
                getArea(false);
                getRegion(false);
                getModule(false);
                getAdminGroup(false);
                getChannel(false);
                $store = getStoreList(false);
                foreach ($store as $key => $val) {
                    getStore($key, false);
                }
                //getMember(false);
                $this->success('更新成功');
                exit;
            }
            if (@in_array('setting', $cache)) {
                $config = M('Setting')->field('set_key')->select();
                foreach ($config as $val) {
                    getConfig($val['set_key'], false);
                }
            }
            if (@in_array('area', $cache)) {
                getArea(false);
            }
            if (@in_array('admin_module', $cache)) {
                getModule(false);
            }
            if (@in_array('admin_group', $cache)) {
                getAdminGroup(false);
            }
            if (@in_array('coupon', $cache)) {
                getCouponList(false);
            }
            if (@in_array('region', $cache)) {
                getRegion(false);
            }
            if (@in_array('store', $cache) || @in_array('store_list', $cache) ) {
                $store = getStoreList(false);
                foreach ($store as $key => $val) {
                    getStore($key, false);
                }
            }           
            if (@in_array('channel', $cache)) {
                getChannel(false);
            }  
            if (@in_array('goods', $cache)) {
                getGoodsList(false);
            }
            $this->success('更新成功');
        } else {
            $this->display();
        }
    }
}